import numpy as np
import pandas as pd

'''
pandas 对字符串进行大小写处理
'''
s = pd.Series(['abcd', 'efg', 'hi'])
print(s.str.upper())
'''
切片处理
'''
print(s.str[0])
print(s.str[-1:0:-2])
'''
string 类型 将费字符串的元素转换成字符串
'''
s = pd.Series([{1: 'temp_1', 2: 'temp_2'}, ['a', 'b'], 0.5, 'my_string'])
print(s.str[1])
s.astype('string').str[1]

s = pd.Series(['a'])
s.str.len()

s = pd.Series([12, 345, 6789])
s.astype('string').str[1]
'''
正则表达式
'''
import re
re.findall(r'Apple','Apple! This Is an Apple!')
re.findall(r'.', 'abc')
re.findall(r'[ac]', 'abc')
re.findall(r'[^ac]', 'abc')
re.findall(r'[ab]{2}', 'aaaabbbb') # {n}指匹配n次
re.findall(r'aaa|bbb', 'aaaabbbb')
re.findall(r'a\\?|a\*', 'aa?a*a')
re.findall(r'a?.', 'abaacadaae')
'''
简写字符集
'''
re.findall(r'.s', 'Apple! This Is an Apple!')
re.findall(r'\w{2}', '09 8? 7w c_ 9q p@')
re.findall(r'\w\W\B', '09 8? 7w c_ 9q p@')
re.findall(r'.\s.', 'Constant dropping wears the stone.')
re.findall(r'上海市(.{2,3}区)(.{2,3}路)(\d+号)',
           '上海市黄浦区方浜中路249号 上海市宝山区密山路5号')
'''
文本处理
'''
s = pd.Series(['上海市黄浦区方浜中路249号',
            '上海市宝山区密山路5号'])
s.str.split('[市区路]')
s.str.split('[市区路]', n=2, expand=True)
print(s.str.rsplit('[市区路]', n=2, expand=True))
'''
合并
'''
s = pd.Series([['a','b'], [1, 'a'], [['a', 'b'], 'c']])
s.str.join('-')
s1 = pd.Series(['a','b'])
s2 = pd.Series(['cat','dog'])
s1.str.cat(s2,sep='-')

s2.index = [1, 2]
s1.str.cat(s2, sep='-', na_rep='?', join='outer')

'''
匹配
'''
s = pd.Series(['my cat', 'he is fat', 'railway station'])
s.str.contains('\s\wat')

s.str.startswith('my')
s.str.endswith('t')
s.str.match('m|h')
s.str[::-1].str.match('ta[f|g]|n') # 反转后匹配
s.str.contains('^[m|h]')
s.str.contains('[f|g]at|n$')
s = pd.Series(['This is an apple. That is not an apple.'])
'''
左侧匹配第一个
'''
s.str.find('apple')
'''
右侧侧匹配第一个
'''
s.str.rfind('apple')
'''
替换
'''
s = pd.Series(['a_1_b','c_?'])
s.str.replace('\d|\?', 'new', regex=True)
s = pd.Series(['上海市黄浦区方浜中路249号',
               '上海市宝山区密山路5号',
               '北京市昌平区北农路2号'])
pat = '(\w+市)(\w+区)(\w+路)(\d+号)'
city = {'上海市': 'Shanghai', '北京市': 'Beijing'}
district = {'昌平区': 'CP District',
            '黄浦区': 'HP District',
            '宝山区': 'BS District'}
road = {'方浜中路': 'Mid Fangbin Road',
        '密山路': 'Mishan Road',
        '北农路': 'Beinong Road'}
def my_func(m):
    str_city = city[m.group(1)]
    str_district = district[m.group(2)]
    str_road = road[m.group(3)]
    str_no = 'No. ' + m.group(4)[:-1]
    return ' '.join([str_city,
                    str_district,
                    str_road,
                    str_no])
s.str.replace(pat, my_func, regex=True)

pat = '(?P<市名>\w+市)(?P<区名>\w+区)(?P<路名>\w+路)(?P<编号>\d+号)'
def my_func(m):
    str_city = city[m.group('市名')]
    str_district = district[m.group('区名')]
    str_road = road[m.group('路名')]
    str_no = 'No. ' + m.group('编号')[:-1]
    return ' '.join([str_city,
                    str_district,
                    str_road,
                    str_no])
s.str.replace(pat, my_func, regex=True)

'''
提取
'''
pat = '(\w+市)(\w+区)(\w+路)(\d+号)'
s.str.extract(pat)
pat = '(?P<市名>\w+市)(?P<区名>\w+区)(?P<路名>\w+路)(?P<编号>\d+号)'
s.str.extract(pat)

s = pd.Series(['A135T15,A26S5','B674S2,B25T6'], index = ['my_A','my_B'])
pat = '[A|B](\d+)[T|S](\d+)'
s.str.extractall(pat)
pat_with_name = '[A|B](?P<name1>\d+)[T|S](?P<name2>\d+)'
s.str.extractall(pat_with_name)

s.str.findall(pat)

'''
常用函数
'''
s = pd.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
s.str.upper()
s.str.lower()
s.str.title()
s.str.capitalize()
s.str.swapcase()


s = pd.Series(['1', '2.2', '2e', '??', '-2.1', '0'])
pd.to_numeric(s, errors='ignore')
pd.to_numeric(s, errors='coerce')
s[pd.to_numeric(s, errors='coerce').isna()]

'''
统计函数
'''
s = pd.Series(['cat rat fat at', 'get feed sheet heat'])
s.str.count('[r|f]at|ee')
s.str.len()

'''
格式型函数
除空型函数
'''
my_index = pd.Index([' col1', 'col2 ', ' col3 '])
my_index.str.strip().str.len()
my_index.str.rstrip().str.len()
my_index.str.lstrip().str.len()

'''
填充型函数
'''
s = pd.Series(['a','b','c'])
s.str.pad(5,'left','*')
s.str.pad(5,'right','*')
s.str.pad(5,'both','*')

s.str.rjust(5, '*')
s.str.ljust(5, '*')
s.str.center(5, '*')

s = pd.Series([7, 155, 303000]).astype('string')
s.str.pad(6,'left','0')
s.str.rjust(6,'0')
s.str.zfill(6)